<template>
  <div class="search_box">
    <i class="iconfont icon-search"></i>
    <!--  @change="to_search" -->
    <input ref="query"  type="search" v-model="query" class="search_input" :placeholder="placeholder" />
    <i class="iconfont icon-clear" v-show="query" @click="clearInput"></i>
  </div>
</template>
<script>
import {debounce} from "@/common/js/util"
export default {
  data() {
    return {
      query: "" //输入的搜索的关键字
    };
  },
  props: {
    placeholder: {
      type: String,
      default: "歌曲，音乐，歌手"
    }
  },
  created() {
    this.$watch("query", debounce((newQuery)=> {
      this.$emit("query", newQuery);
    },200));
  },
  methods: {
    //监听input的框的值得变化,并传递给父组件
    // to_search(e){
    //   let keyCode = e.target.value;
    //   this.$emit('Search', keyCode);
    // },
    //点击x清空input框
    clearInput() {
      this.query = "";
    },
    //设置query的方法
    setQuery(query) {
      this.query = query;
    },
    blur(){
      this.$refs.query.blur()
    }
  }
};
</script>
<style lang="stylus"  rel="stylesheet/stylus">
@import '../../common/stylus/variable.styl';

.search_box {
  width: 100%;
  height: 40px;
  background: $color-highlight-background;
  border-radius: 10px;
  display: grid;
  grid-template-columns: 24px auto;
  padding: 10px;
  grid-template-columns: 1fr 11fr 1fr;

  .search_input {
    width: 100%;
    outline: none;
    background: $color-highlight-background;
    border: none;
    color: $color-text;
    font-size: $font-size-medium;

    &::placeholder {
      color: $color-text-d;
    }
  }

  input:focus {
    border: 1px solid $color-theme;
  }

  i {
    text-align: center;
  }

  input::-webkit-search-cancel-button {
    display: none;
  }

  // --清除IE下的 search 叉号
  input[type=search]::-ms-clear {
    display: none;
  }
}
</style>
